import { Inject, Injectable } from '@nestjs/common';
import { CreateTagDto } from './dto/create-tag.dto';
import { UpdateTagDto } from './dto/update-tag.dto';
import { PrismaService } from 'src/prisma/prisma.service';
import type {TagType, ItemType} from '@prisma/client'
@Injectable()
export class TagService {
  @Inject(PrismaService) private prismaService: PrismaService;
  create(createTagDto: CreateTagDto) {
    return 'This action adds a new tag';
  }

  async findAllTags(userId: number) {
    const [systemExpensesTags, systemIncomeTags, customExpensesTags, customIncomeTags,] = await Promise.all([
      this.getAllSystemTags('expenses'),
      this.getAllSystemTags('income'),
      this.getUserTagsByKind('expenses', userId),
      this.getUserTagsByKind('income', userId),
    ]);
    return {
      data: {
        expensesTags: [...systemExpensesTags, ...customExpensesTags],
        incomeTags: [...systemIncomeTags, ...customIncomeTags],
      }
    };
  }

  findOne(id: number) {
    return `This action returns a #${id} tag`;
  }

  update(id: number, updateTagDto: UpdateTagDto) {
    return `This action updates a #${id} tag`;
  }

  remove(id: number) {
    return `This action removes a #${id} tag`;
  }

  getAllSystemTags(kind: ItemType) {
    return this.prismaService.tag.findMany({
      where: {
        kind,
        type: 'system',
      },
    });
  }
  getUserTagsByKind(kind: ItemType, userId: number) {
    if(!userId) return []
    return this.prismaService.tag.findMany({
      where: {
        kind,
        type: 'custom',
        userId,
      },
    });
  }
}
